import 'dart:async';
import 'package:flutter/services.dart';
import 'dart:ffi'; // For FFI
import 'dart:io'; // For Platform.isX
import 'package:ffi/ffi.dart';

final DynamicLibrary nativeLib = Platform.isAndroid
    ? DynamicLibrary.open("libsome.so")
    : DynamicLibrary.open("some.framework/some");

final int Function(int x, int y) nativeAdd = nativeLib
    .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add")
    .asFunction();

typedef reverse_func = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length);
typedef Reverse = Pointer<Utf8> Function(Pointer<Utf8> str, int length);

final reversePointer = nativeLib.lookup<NativeFunction<reverse_func>>('reverse');
final reverse = reversePointer.asFunction<Reverse>();

class FlutterNoCppSrc {
  // static const MethodChannel _channel =
  // const MethodChannel('flutter_no_cpp_src');
  //
  // static Future<String> get platformVersion async {
  //   final String version = await _channel.invokeMethod('getPlatformVersion');
  //   return version;
  // }

  static int addWithCpp(int x, int y) {
    return nativeAdd(x, y);
  }

  static String getNetParams(String params){
    final reversedMessage = Utf8.fromUtf8(reverse(Utf8.toUtf8(params), params.length));
    return reversedMessage;
  }

}

